Building a Tip of the Day Application (Part 3): Submitting Tips to the Web Service
Table of Contents
Formatting Dates
Before assembling the rest of the DateChooser (calendar) component pieces, you need to add a function to convert dates from a Flash date format (zero-based) to a ColdFusion date format. (A ColdFusion component (CFC) provides the web service at http://www.flash-mx.com/ws/tipoftheday/tip.cfc?wsdl.)
Assign a date to this.dataHolder_dh.tipdate_str as follows:
- Click the presentation screen to select it.
-
Open the Actions panel and add the following function within the
onClipEvent (load)function:function dateFormat(dDate:Date):String { var nYear = dDate.getFullYear(); var nMonth = dDate.getMonth()+1; var nDay = dDate.getDate(); if (nMonth<10) { nMonth = "0"+nMonth; } if (nDay<10) { nDay = "0"+nDay; } return new String(nMonth+"/"+nDay+"/"+nYear); }This little function takes a single parameter, a date, and extracts the years, months, and days. It gives them leading zeros if the months or days are less than ten, and returns a new string back to the caller. Also, note that you are adjusting the month and adding 1 to the specified month to make the dates compatible with ColdFusion.
-
Modify the original code in the
onClipEvent (load)method to use the new dateFormat function to set a default date in the DataHolder instance. Change the code from:this.dataHolder_dh.tipdate_str = "09/04/2003";
to
this.dataHolder_dh.tipdate_str = dateFormat( new Date() );This sets a default date in the DataHolder component to the current date rather than to the hard-coded "09/04/2003" date.
- Test the application.
When you have changed the code, test the SWF to make sure everything works. Now the most recent tip is displayed instead of the tip from the hard-coded date.